//package com.weirblog.resource;
//import org.patchca.color.ColorFactory;
//import org.patchca.filter.predefined.*;
//import org.patchca.service.ConfigurableCaptchaService;
//import org.patchca.utils.encoder.EncoderHelper;
//import org.patchca.word.RandomWordFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.http.codec.EncoderHttpMessageWriter;
//import org.springframework.stereotype.Controller;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.ResponseBody;
//
//import com.weirblog.entity.Users;
//import com.weirblog.service.UsersService;
//import com.weirblog.util.Json;
//
//import javax.servlet.http.Cookie;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//import java.awt.Color;
//import java.io.IOException;
//import java.util.Random;
//
//@Controller
//public class PatchcaController {
//	@Autowired
//	private UsersService usersService;
//	
//    private static ConfigurableCaptchaService cs = new ConfigurableCaptchaService();
//    private static Random random = new Random();
//    static {
//        cs.setColorFactory(new ColorFactory() {
//            @Override
//            public Color getColor(int x) {
//                int[] c = new int[3];
//                int i = random.nextInt(c.length);
//                for (int fi = 0; fi < c.length; fi++) {
//                    if (fi == i) {
//                        c[fi] = random.nextInt(71);
//                    } else {
//                        c[fi] = random.nextInt(256);
//                    }
//                }
//                return new Color(c[0], c[1], c[2]);
//            }
//        });
//        RandomWordFactory wf = new RandomWordFactory();
//        wf.setCharacters("23456789abcdefghigkmnpqrstuvwxyzABCDEFGHIGKLMNPQRSTUVWXYZ");
//        wf.setMaxLength(4);
//        wf.setMinLength(4);
//        cs.setWordFactory(wf);
//    }
//    @ResponseBody
//    @RequestMapping("/pcrimg")
//    public JsonVO crimg(HttpServletRequest request, HttpServletResponse response,String email) throws IOException {
//    	JsonVO json = new JsonVO();
//    	Users u = usersService.findByEmail(email);
//    	if (u!=null) {
//			json.setSuccess(true);
//			json.setMsg("Y");
//		}else {
//			switch (random.nextInt(5)) {
//            case 0:
//                cs.setFilterFactory(new CurvesAbstractRippleFilterFactory(cs.getColorFactory()));
//                break;
//            case 1:
//                cs.setFilterFactory(new MarbleAbstractRippleFilterFactory());
//                break;
//            case 2:
//                cs.setFilterFactory(new DoubleRippleFilterFactory());
//                break;
//            case 3:
//                cs.setFilterFactory(new WobbleAbstractRippleFilterFactory());
//                break;
//            case 4:
//                cs.setFilterFactory(new DiffuseAbstractRippleFilterFactory());
//                break;
//        }
//        /*HttpSession session = request.getSession(false);
//        if (session == null) {
//            session = request.getSession();
//        }*/
//        //setResponseHeaders(response);
//        String token = EncoderHelper.write(cs, "png", response.getOutputStream());
////        String token = EncoderHelper.getChallangeAndWriteImage(cs, "png", response.getOutputStream());
//        
//        Cookie cookie = new Cookie("emailCheck", token);
//        cookie.setMaxAge(60*10);//十分钟
//        cookie.setPath("/");
//        response.addCookie(cookie);
//        
//        try {
//			//EmailSender.send(email, "爱威尔——验证码","爱威尔博客验证码："+token);
//			json.setSuccess(true);
//		} catch (Exception e) {
//			json.setMsg("验证码获取失败，请重试看看");
//			e.printStackTrace();
//		}
//		}
//    	
//        return json;
////        session.setAttribute("captchaToken", token);
////        System.out.println("当前的SessionID=" + session.getId() + "，验证码=" + token);
//    }
//    protected void setResponseHeaders(HttpServletResponse response) {
//        response.setContentType("image/png");
//        response.setHeader("Cache-Control", "no-cache, no-store");
//        response.setHeader("Pragma", "no-cache");
//        long time = System.currentTimeMillis();
//        response.setDateHeader("Last-Modified", time);
//        response.setDateHeader("Date", time);
//        response.setDateHeader("Expires", time);
//    }
//    @ResponseBody
//    @RequestMapping("/checkImg")
//    public JsonVO checkImg(String checkImg,HttpServletRequest request) {
//    	JsonVO j = new JsonVO();
//		Cookie[] cookies = request.getCookies();
//		for (Cookie cookie : cookies) {
//			if ("emailCheck".equals(cookie.getName())) {
//				if (cookie.getValue().equals(checkImg.trim())) {
//					j.setSuccess(true);
//					return j;
//				}
//			}
//		}
//    	return j;
//	}
//}